<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>

    <template id="temp">
        <h2>{{info.name}}</h2>
        <button @click="changeInfo">修改Info</button>
        <button @click="changeInfoName">修改Info.name</button>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const App = {
            template:"#temp",
            data() {
                return {
                    info: {
                        name: 'ACE',
                        age: 18
                    }
                }
            },
            watch: {
                //默认情况下我们的侦听器只会针对监听的数据本身的改变(内部发生改变不能监听 需要deep)
                // info(newval, oldval) {
                //     console.log("新值：", newval, "旧值：", oldval)
                // }

                info: {
                    //上面直接写方法是语法糖
                    handler(newval, oldval) {
                        //这里旧值和新值是一样的 需要深拷贝对象
                        console.log("新值：", newval, "旧值：", oldval)
                    },
                    deep: true,   //深度监听
                    // immediate: true   //立即执行
                }
            },
            methods: {
                changeInfo() {
                    this.info = {name: 'Sabo', age: 18}
                },
                changeInfoName() {
                    this.info.name = 'Luffy'
                }
            },
        }

        Vue.createApp(App).mount("#app")
    </script>
</body>
</html>